package com.controller;


import com.entity.Admin;
import com.entity.Company;
import com.entity.User;
import com.service.LoginService;
import com.service.impl.LoginServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/CheckZhuceServlet")
public class CheckZhuceServlet extends HttpServlet {
    private LoginService loginService= new LoginServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        super.doPost(req, resp);
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        String name=req.getParameter("name");
        String pwd=req.getParameter("pwd");
        String entity=req.getParameter("entity");
        String id = req.getParameter("id");
        System.out.println("entity = " + entity);
        User user;
        Company company;
        if ("求职者".equals(entity)){
            user=loginService.selectAllUser(id,pwd);
            if (user==null){
                user = new User();
                user.setName(name);
                user.setPwd(pwd);
                user.setUserId(id);
                int i = loginService.addUser(user);
                System.out.println("i3 = " + i);
                if(i==1){
                    System.out.println(" success1" );
                    resp.getWriter().write("success");
                    System.out.println(" success2" );
                }else{
                    resp.getWriter().write("fail");
                }

            }

        }else if("招聘者".equals(entity)){
            company=loginService.selectAllCompany(id,pwd);

            if (company==null){
                company = new Company();
                company.setCompanyId(id);
                company.setCompanyName(name);
                company.setPwd(pwd);
                int i = loginService.addCompany(company);
                if(i==1){
                    resp.getWriter().write("success");
                }else{
                    resp.getWriter().write("fail");
                }
            }

        }else {
            resp.getWriter().write("fail");
        }
    }
}
